Skip to content

Conversation

@hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Oct 2, 2024

# https://github.com/python/mypy/issues/17864
def f(x: str | int) -> None:
if x == "x":
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this desired behavior? I feel like we should narrow to Literal["x"]. Yes, it might be a subclass of int that compares equal with str, but don't do that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this case seems fine. But classes that compare equal to int are not unusual (e.g. Decimal), so it's not clear where to draw the line. We might need some ad-hoc rules to support common use cases.

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last time I tried that change IIRC it looked pretty good on primer, I think the main obstacle was it exposed some shortcomings in the binder

@hauntsaninja hauntsaninja merged commit 4e4826f into python:master Oct 5, 2024
14 checks passed
@hauntsaninja hauntsaninja deleted the regr-test branch October 5, 2024 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants